
import down from '@/utils/down'
import axios from "axios";
import store from '@/store'
import { getToken } from '@/utils/auth'
let ROOT;
if (process.env.NODE_ENV === 'development') {
  //ROOT = "/api"
  ROOT = "http://192.168.1.158:7778/fmis"
} else {
  ROOT = window.ROOT;
}
var download= {
  exportExcel(url, data,filename) {
    var p = new Promise(function(resolve, reject){        //做一些异步操作
      // axios({
      //   method: "post",
      //   baseURL: ROOT,
      //   withCredentials: true,
      //   responseType:"blob",
      //   timeout: 20000 ,
      //   url: url,
      //   headers: {
      //     'Access-Control-Allow-Origin': '*',
      //     'Content-Type': 'application/json',
      //     token:store.getters.token?getToken():null
      //   },
      //   data: data
      // })
      down({
        url: url,
        method: "post",
        data
      })
        .then(response => {
          console.log(response,'返回111')
          const blob = new Blob([response.data],{ type: "application/vnd.ms-excel"});
          var file_name = filename||this.getDateTime()+".xls";
          const linkNode = document.createElement('a');
          linkNode.download = file_name; //a标签的download属性规定下载文件的名称
          linkNode.style.display = 'none';
          linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
          document.body.appendChild(linkNode);
          linkNode.click();  //模拟在按钮上的一次鼠标单击
  
          URL.revokeObjectURL(linkNode.href); // 释放URL 对象
          document.body.removeChild(linkNode);
          resolve("成功")
        }).catch(function (error) {
          console.log(error);
          reject('失败')
          
        });
    });
  return p;
    
  },
  // exportExcel: function (url, data,filename) {
  //   axios({
  //     method: "post",
  //     baseURL: ROOT,
  //     withCredentials: true,
  //     responseType:"blob",
  //     timeout: 20000 ,
  //     url: url,
  //     headers: {
  //       'Access-Control-Allow-Origin': '*',
  //       'Content-Type': 'application/json',
  //       token:store.getters.token?getToken():null
  //     },
  //     data: data
  //   })
  //     .then(response => {
  //       const blob = new Blob([response.data],{ type: "application/vnd.ms-excel"});
  //       var file_name = filename||this.getDateTime()+".xls";
  //       const linkNode = document.createElement('a');

  //       linkNode.download = file_name; //a标签的download属性规定下载文件的名称
  //       linkNode.style.display = 'none';
  //       linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
  //       document.body.appendChild(linkNode);
  //       linkNode.click();  //模拟在按钮上的一次鼠标单击

  //       URL.revokeObjectURL(linkNode.href); // 释放URL 对象
  //       document.body.removeChild(linkNode);
  //     }).catch(function (error) {
  //       console.log(error);
  //     });
  // },
  getDateTime:function(){
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return year+''+month+''+day+''+hour+''+minute+''+second
  }
}
export default download;
